<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline :label-width="80">
                <el-form-item label="颜色" prop="color">
                    <el-input class="w-[280px]" v-model="queryParams.color" clearable placeholder="请输入颜色" />
                </el-form-item>
                <el-form-item label="型号" prop="type">
                    <el-input class="w-[280px]" v-model="queryParams.type" clearable placeholder="请输入型号" />
                </el-form-item>
                <el-form-item label="盖帽颜色" prop="hat_color">
                    <el-input class="w-[280px]" v-model="queryParams.hat_color" clearable placeholder="请输入盖帽颜色" />
                </el-form-item>
                <el-form-item label="接口口径" prop="caliber">
                    <el-input class="w-[280px]" v-model="queryParams.caliber" clearable placeholder="请输入接口口径" />
                </el-form-item>
                <el-form-item label="尺寸" prop="size">
                    <el-input class="w-[280px]" v-model="queryParams.size" clearable placeholder="请输入尺寸" />
                </el-form-item>
                <el-form-item label="进水方式" prop="method">
                    <el-input class="w-[280px]" v-model="queryParams.method" clearable placeholder="请输入进水方式" />
                </el-form-item>
                <el-form-item label="特殊型号" prop="special_type">
                    <el-input class="w-[280px]" v-model="queryParams.special_type" clearable placeholder="请输入特殊型号" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="flex" style="margin-bottom: 10px;">
                <el-button type="primary" @click="handleAdd">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
                <export-data
                    class="ml-2.5"
                    :fetch-fun="goodsLists"
                    :page-size="pager.size"
                    :params="queryParams"
                    :isAddress="false"
                />
            </div>
            
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange" :fit="true">
                    <!-- <el-table-column type="selection" width="55" align="center"/> -->
                    <el-table-column label="名称" prop="name" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="分类" prop="cid" show-overflow-tooltip align="center" width="160" />
                    <!-- <el-table-column label="封面图" prop="cover" show-overflow-tooltip >
                      <template #default="{ row }">
                          <el-image :src="row.cover" style="width: 100px; height: 100px;" :fit="row.cover.includes('http') ? 'cover' : 'contain'"/>
                      </template>
                  </el-table-column> -->
                    <el-table-column label="颜色" prop="color" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="型号" prop="type" show-overflow-tooltip align="center" min-width="200" />
                    <el-table-column label="盖帽颜色" prop="hat_color" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="接口口径" prop="caliber" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="系列" prop="series" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="尺寸" prop="size" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="进水方式" prop="method" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="特殊型号" prop="special_type" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="销售单价" prop="sale_price" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="成本单价" prop="cost_price" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="单位" prop="unit" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="商品编码" prop="sku_code" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="库存" prop="stock" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="简介" prop="desc" show-overflow-tooltip align="center" width="160" />
                    <el-table-column label="操作" width="280" fixed="right" align="center">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button v-if="row.status == 1" type="danger" link @click="handleDelete(row.id)">
                                删除
                            </el-button>
                            <el-button v-if="row.status == 0" type="primary" link @click="handleRecover(row.id)">
                                恢复
                            </el-button>
                            <el-button type="primary" link @click="handleInventoryEdit(row)">
                                <!-- <router-link :to="{ path: getRoutePath('goods.inventory'), query: { id: row.id } }"> -->
                                    库存管理
                                <!-- </router-link> -->
                            </el-button>
                            <el-button type="primary" link >
                                <router-link :to="{ path: getRoutePath('goods.inventoryLog'), query: { id: row.id } }">
                                    库存操作日志
                                </router-link>
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
        <InventoryEdit v-if="showInventoryEdit" ref="inventoryEditRef"  @success="getLists" @close="showInventoryEdit = false" />
    </div>
</template>

<script lang="ts" setup name="hotelBasicLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { goodsLists, goodsDelete, goodsRecover } from '@/api/goods'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'
import { shallowRef, ref, reactive, nextTick } from 'vue'
// 商品编辑
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 库存管理
import InventoryEdit from './inventoryEdit.vue'
const inventoryEditRef = shallowRef<InstanceType<typeof InventoryEdit>>()
// 是否显示编辑框
const showInventoryEdit = ref(false)

// 查询条件
const queryParams = reactive({
    color: '',
    type: '',
    hat_color: '',
    caliber: '',
    size: '',
    method: '',
    special_type: '',
    series: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: goodsLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

// 编辑库存
const handleInventoryEdit = async (data: any) => {
    showInventoryEdit.value = true
    await nextTick()
    inventoryEditRef.value?.open('edit')
    inventoryEditRef.value?.setFormData(data)
  }
// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await goodsDelete({ id })
    getLists()
}

// 恢复
const handleRecover = async (id: number) => {
    await feedback.confirm('确定要恢复？')
    await goodsRecover({ id })
    getLists()
}

getLists()
</script>
